﻿using HirelSeriesH.Entity;
using HirelSeriesH.Tools;
using MySql.Data.MySqlClient;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HirelSeriesH
{
    public partial class SettingsForm : UIForm
    {
        List<UIDoubleUpDown> Power01 = new List<UIDoubleUpDown>();
        List<UIDoubleUpDown> Power02 = new List<UIDoubleUpDown>();
        List<UIDoubleUpDown> Power03 = new List<UIDoubleUpDown>();
        List<UIDoubleUpDown> Power04 = new List<UIDoubleUpDown>();
        List<UIDoubleUpDown> Power05 = new List<UIDoubleUpDown>();
        /*List<NumericUpDown> Power06 = new List<NumericUpDown>();
        List<NumericUpDown> Power07 = new List<NumericUpDown>();
        List<NumericUpDown> Power08 = new List<NumericUpDown>();*/

        List<List<UIDoubleUpDown>> PowerList = new List<List<UIDoubleUpDown>>();

        public SettingsForm()
        {
            InitializeComponent();

            Power01.Add(VgMaxVoltage01);
            Power01.Add(VgMaxCurrent01);
            Power01.Add(VgBHDZ01);

            Power02.Add(VgMaxVoltage02);
            Power02.Add(VgMaxCurrent02);
            Power02.Add(VgBHDZ02);

            Power03.Add(VgMaxVoltage03);
            Power03.Add(VgMaxCurrent03);
            Power03.Add(VgBHDZ03);

            Power04.Add(VgMaxVoltage04);
            Power04.Add(VgMaxCurrent04);
            Power04.Add(VgBHDZ04);

            Power05.Add(VgMaxVoltage05);
            Power05.Add(VgMaxCurrent05);
            Power05.Add(VgBHDZ05);

            /*Power06.Add(VgMaxVoltage06);
            Power06.Add(VgMaxCurrent06);
            Power06.Add(VgBHDZ06);

            Power07.Add(VgMaxVoltage07);
            Power07.Add(VgMaxCurrent07);
            Power07.Add(VgBHDZ07);

            Power08.Add(VgMaxVoltage08);
            Power08.Add(VgMaxCurrent08);
            Power08.Add(VgBHDZ08);*/

            PowerList.Add(Power01);
            PowerList.Add(Power02);
            PowerList.Add(Power03);
            PowerList.Add(Power04);
            PowerList.Add(Power05);
            /*PowerList.Add(Power06);
            PowerList.Add(Power07);
            PowerList.Add(Power08);*/
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < Global.Global.PowerList.Count; i++)
            {
                Power power = Global.Global.PowerList[i];
                List<UIDoubleUpDown> numericUps = PowerList[i];

                numericUps[0].Value = (double)power.MaxVoltage;
                numericUps[1].Value = (double)power.MaxCurrent;
                numericUps[2].Value = (double)power.BHDZ;
            }
            for (int i = 0; i < Global.Global.PowerVgList.Count; i++)
            {
                VGPower vgpower = Global.Global.PowerVgList[i];
                List<UIDoubleUpDown> numericUps = PowerList[i + 4];

                numericUps[0].Value = (double)vgpower.MaxVoltage;
                numericUps[1].Value = (double)vgpower.MaxCurrent;
                numericUps[2].Value = (double)vgpower.BHDZ;
            }
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < Global.Global.PowerList.Count; i++)
            {
                List<UIDoubleUpDown> numericUps = PowerList[i];
                //更新全局变量.
                Global.Global.PowerList[i].MaxVoltage = (decimal)numericUps[0].Value;
                Global.Global.PowerList[i].MaxCurrent = (decimal)numericUps[1].Value;
                Global.Global.PowerList[i].BHDZ = (decimal)numericUps[2].Value;
                //更新数据库 
                MySqlParameter[] para = new MySqlParameter[] {
                    new MySqlParameter("@ID", Global.Global.PowerList[i].ID),
                    new MySqlParameter("@MaxVoltage", Global.Global.PowerList[i].MaxVoltage),
                    new MySqlParameter("@MaxCurrent", Global.Global.PowerList[i].MaxCurrent),
                    new MySqlParameter("@BHDZ", Global.Global.PowerList[i].BHDZ)};

                DBHelper.ExecuteNonQuery(DBSql.UPDATE_HTRB_POWER_SETTING, 1, para);
            }

            for (int i = 0; i < Global.Global.PowerVgList.Count; i++)
            {
                List<UIDoubleUpDown> numericUps = PowerList[i + 4];
                //更新全局变量.
                Global.Global.PowerVgList[i].MaxVoltage = (decimal)numericUps[0].Value;
                Global.Global.PowerVgList[i].MaxCurrent = (decimal)numericUps[1].Value;
                Global.Global.PowerVgList[i].BHDZ = (decimal)numericUps[2].Value;
                //更新数据库 
                MySqlParameter[] para = new MySqlParameter[] {
                    new MySqlParameter("@ID", Global.Global.PowerVgList[i].ID),
                    new MySqlParameter("@MaxVoltage", Global.Global.PowerVgList[i].MaxVoltage),
                    new MySqlParameter("@MaxCurrent", Global.Global.PowerVgList[i].MaxCurrent),
                    new MySqlParameter("@BHDZ", Global.Global.PowerVgList[i].BHDZ)};

                DBHelper.ExecuteNonQuery(DBSql.UPDATE_HTRB_POWER_SETTING, 1, para);
            }

            UIMessageDialog.ShowMessageDialog(this, "设置保存成功", UILocalize.InfoTitle, false, UIStyle.Colorful);
        }

        private void BtnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
